<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->controller = $this;
    }

}

class Pre_Controller extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        //$this->output->enable_profiler(TRUE);
        $this->load->Model('Config_model');
        $this->load->Model('Menu_model');
        $data['islogin']=$this->Check();
        $data['goods_cart_html']=$this->get_cart_count();
        $data['goods_cart_fun']=$this->get_cart_click_fun();
        $data['top_menu_list']=$this->get_nav_menu();
        $data['config_model']=$this->get_config();
        $data['contact_model']=$this->get_contact();
        $this->load->vars($data);
    }

    public function get_max_dept_first_menu($menu_model,&$one)
    {
        if($menu_model->child==1)
        {
            $child_arr_menuids= explode(',',trim($menu_model->arrchild,','));
            $child_menu=$this->Menu_model->get_menu_by_id($child_arr_menuids[1]);
            $this->get_max_dept_first_menu($child_menu,$one);
        }else{
            $one=$menu_model;
        }
    }

    public function get_nav_menu()
    {
        $top_menu_list=$this->Menu_model->get_all_top_menu();

        //栏目
        foreach($top_menu_list as $top_menu_list_item)
        {
            if($top_menu_list_item->child==1 && $top_menu_list_item->isdrop==1)
            {
                $top_menu_list_item->child_list=$this->Menu_model->get_all_child_menu_by_id($top_menu_list_item->menuid);
                foreach($top_menu_list_item->child_list as $top_menu_list_item_childs_item)
                {
                    $first_child_menu_model=null;
                    $this->get_max_dept_first_menu($top_menu_list_item_childs_item,$first_child_menu_model);
                    if(!empty( $first_child_menu_model))
                    {
                        $top_menu_list_item_childs_item->page_url=site_url($first_child_menu_model->menu_control."/".$first_child_menu_model->menu_action.($first_child_menu_model->page_type==1?'list':'info')."/".$first_child_menu_model->menuid);
                    }
                }
            }
            $first_child_menu_model=null;
            $this->get_max_dept_first_menu($top_menu_list_item,$first_child_menu_model);
            if(!empty( $first_child_menu_model))
            {
                $top_menu_list_item->page_url=site_url($first_child_menu_model->menu_control."/".$first_child_menu_model->menu_action.($first_child_menu_model->page_type==1?'list':'info')."/".$first_child_menu_model->menuid);
            }

        }
        return $top_menu_list;
    }

    public function get_high_item_left_nav_crumbs($menuid,&$left_nav,&$top_nav_title,&$crumbs,&$icon)
    {
        $menu_model=$this->Menu_model->get_menu_by_id($menuid);
        $array_menuids=explode(',',trim($menu_model->path,','));
        $left_nav=$this->Menu_model->get_childs_by_parentid($array_menuids[0]);
        $top_menu=$this->Menu_model->get_menu_by_id($array_menuids[0]);
        $crumbs=$this->get_crumbs($array_menuids);
        $top_nav_title=$top_menu->title;
        $icon=$top_menu->icon;
        foreach($left_nav as $left_nav_item)
        {
            $first_child_menu_model=null;
            $this->get_max_dept_first_menu($left_nav_item,$first_child_menu_model);
            if(!empty( $first_child_menu_model))
            {
                $left_nav_item->page_url=site_url($first_child_menu_model->menu_control."/".$first_child_menu_model->menu_action.($first_child_menu_model->page_type==1?'list':'info')."/".$first_child_menu_model->menuid);
            }
        }
        return $array_menuids;
    }
    public function get_crumbs($array_menuids)
    {
        $crumbs=array();
        foreach($array_menuids as $array_menuids_item)
        {
            $menu_model=$this->Menu_model->get_menu_by_id($array_menuids_item);
            $first_child_menu_model=null;
            $this->get_max_dept_first_menu($menu_model,$first_child_menu_model);
            if(!empty($first_child_menu_model))
            {
                $menu_model->page_url=site_url($first_child_menu_model->menu_control."/".$first_child_menu_model->menu_action.($first_child_menu_model->page_type==1?'list':'info')."/".$first_child_menu_model->menuid);
            }
            $crumbs[]=$menu_model;

        }
        return $crumbs;
    }


    public function create_static_html()
    {

    }

    public function get_config()
    {
        $this->load->Model('Config_model');
        return $this->Config_model-> Get_Config_FirstRow();
    }

    public function get_contact()
    {
        $this->load->Model('Contact_model');
        return $this->Contact_model->get_first_row();
    }


    //一下没用
    public function GetContactModel()
    {
        $contactModel=$this->Contact_model->Get_Contact_First();
        return $contactModel;
    }
    public function GetConfigModel()
    {
        return $this->Config_model->Get_Config_FirstRow();
    }
    public function Check()
    {
        $CI =& get_instance();
        $CI->load->library('session');
        if($CI->session->has_userdata('account'))
        {
            return true;
        }
        return false;
    }

    public function check_redirect($bacrurl)
    {
        //echo $bacrurl;die;
        if(!$this->Check())
        {
            if(empty($bacrurl))
            {
                Page_Redirect('customer/r_login');
            }
            else
            {
                header('Content-Type:text/html;charset=utf-8');
                $url = site_url('customer/r_login').'?backurl='.$bacrurl;
                echo "<script type='text/javascript'>location.href='$url'</script>";
                die;
            }
        }
    }
    public function frame_check_redirect()
    {
        if(!$this->Check())
        {
            Frame_Page_Redirect('customer/r_login');
        }
    }

    public function ajax_check_redirect()
    {
        if(!$this->Check())
        {
            echo 0;
        }else{
            echo 1;
        }
    }
    public function get_customer_id()
    {
        //$this->check_redirect();
        $CI =& get_instance();
        $CI->load->library('session');
        return $this->session->userdata('customer_id');
    }
    public function get_cart_count()
    {
        $this->load->Model('Goods_cart_model');
        $customer_id=$this->get_customer_id();
        if(!empty($customer_id))
        {
            $res= $this->Goods_cart_model->get_goods_cart_count($customer_id);
            if(!empty($res)){
                $car_count_html='<span onclick="get_cartlist()">'.$res->cart_count.'</span>';
            }
            else{
                $car_count_html='';
            }
        }else{
            $car_count_html='';
        }
        return $car_count_html;
    }
    public function get_cart_click_fun()
    {
        $this->load->Model('Goods_cart_model');
        $customer_id=$this->get_customer_id();
        if(!empty($customer_id))
        {
            $car_count_html='get_cartlist()';
        }else{
            $car_count_html='void(0)';
        }
        return $car_count_html;
    }
}

class Is_LoginController extends Pre_Controller
{
    public function __construct()
    {
        parent::__construct();

    }



}

//商城控制器
class Shop_Controller extends Is_LoginController
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Goods_model');
        $this->load->Model('Size_model');
        $this->load->Model('Color_model');
        $this->load->Model('Good_material_model');
        $this->load->Model('Goods_style_model');
        $this->load->Model('Color_model');
        $data=$this->get_goods_attribute();
        $data['is_search']=0;
        $mark=array('material'=>0,'color'=>0,'style'=>0);
        $data['mark']=$mark;
        $this->load->vars($data);
    }

    public function get_goods_attribute()
    {
        //获取所有尺码
        $data['sizelist']=$this->Size_model->get_allsize();

        //获取所有材质
        $data['materiallist']=$this->Good_material_model->get_allgood_material();
        //获取所有颜色
        $data['colorlist']=$this->Color_model->get_allcolor();
        //获取所有款式
        $data['goodsstyle']=$this->Goods_style_model->get_allgoods_style();
        return $data;
    }

    public function return_search_url($key,$val,$search_url)
    {

        $_url=site_url('search/index');
        $arr = parse_url($search_url);//分解url host 路径 参数
        $arr_query =$this->convertUrlQuery($arr['query']);//获取参数并重新格式化参数，如有相同参数名，则该参数的值取最后一个值

        $arr_keys=array_keys($arr_query);//取参数的索引键

        $index=0;

        foreach($arr_keys as $arr_keys_item)//遍历各个索引键
        {

            if(strtolower($arr_keys_item)===strtolower($key))//如果该索引键与传的参数键相同，跳过继续执行该方法
            {
                continue;
            }
            if($index==0)
            {
                $_url.="?". $arr_keys_item."=".$arr_query["$arr_keys_item"];

            }
            else
            {
                $_url.="&". $arr_keys_item."=".$arr_query["$arr_keys_item"];
            }
            $index++;
        }

        $_url.= count($arr_query)>1?"&$key=$val":"?$key=$val";
        return $_url;
    }
    function convertUrlQuery($query)
    {

        $queryParts = explode('&', $query);//分割

        $params = array();
        foreach ($queryParts as $param) {

            $item = explode('=', $param);

            if(empty($item[0]) || empty($item[1])) continue;


            $params[$item[0]] = $item[1];//如果参数名相同，取最后一个值 如 material=4&material=1 怎返回material=1

        }
        return $params;
    }
}

class ShopSearch_Controller extends Shop_Controller
{
    public function __construct()
    {
        parent::__construct();
        $data['is_search']=1;
        $this->load->vars($data);
    }

    public function get_sql_where($arr_query,&$mark=array())
    {
        $arr_query_key=  array_keys($arr_query);
        $sql_where="";
        foreach($arr_query_key as $arr_query_key_item)
        {
            $val=urldecode($arr_query["$arr_query_key_item"]);
            switch($arr_query_key_item)
            {
                case "key":
                    $sql_where.=" and title like '%$val%' ";
                    break;
                case "color":
                    $sql_where.=" and goods_id in (SELECT goods_color.goods_id FROM goods_color where color_id=$val)";
                    $mark['color']=$val;
                    break;
                case "style":
                    $sql_where.=" and goods_style_id=$val";
                    $mark['style']=$val;
                    break;
                case "material":
                    $sql_where.=" and good_material_id=$val";
                    $mark['material']=$val;
                    break;
                default:break;
            }
        }
        return $sql_where;
    }

    public function del_condition($arr_query,&$mark)
    {
        $arr_query_key=  array_keys($arr_query);
        $condition_key_val=array();
        if(count($arr_query)>0)
        {
            $mark=1;
            foreach($arr_query_key as $arr_query_key_item)
            {
                switch($arr_query_key_item)
                {
                    case "material":
                        $temp_arr_keys=$arr_query_key;
                        $key=array_search($arr_query_key_item,$temp_arr_keys);
                        unset($temp_arr_keys["$key"]);
                        $this->load->Model('Good_material_model');
                        $title=$this->Good_material_model->get_title($arr_query["$arr_query_key_item"])->title;
                        $temp_arr_str=site_url('search/index');
                        $index=0;
                        foreach($temp_arr_keys as $temp_arr_keys_item)
                        {
                            if($index==0)
                            {
                                $temp_arr_str.="?$temp_arr_keys_item=".$arr_query["$temp_arr_keys_item"];
                            }
                            else
                            {
                                $temp_arr_str.="&$temp_arr_keys_item=".$arr_query["$temp_arr_keys_item"];
                            }
                            $index++;
                        }
                        $condition_key_val[]=array('title'=>$title,'url'=>$temp_arr_str);
                        break;
                    case "style":

                        $temp_arr_keys=$arr_query_key;
                        $key=array_search($arr_query_key_item,$temp_arr_keys);
                        unset($temp_arr_keys["$key"]);
                        $this->load->Model('Goods_style_model');
                        $title=$this->Goods_style_model->get_title($arr_query["$arr_query_key_item"])->title;
                        $temp_arr_str=site_url('search/index');
                        $index=0;
                        foreach($temp_arr_keys as $temp_arr_keys_item)
                        {
                            if($index==0)
                            {
                                $temp_arr_str.="?$temp_arr_keys_item=".$arr_query["$temp_arr_keys_item"];
                            }
                            else
                            {
                                $temp_arr_str.="&$temp_arr_keys_item=".$arr_query["$temp_arr_keys_item"];
                            }
                            $index++;
                        }
                        $condition_key_val[]=array('title'=>$title,'url'=>$temp_arr_str);
                        break;
                    case "color":
                        $temp_arr_keys=$arr_query_key;
                        $key=array_search($arr_query_key_item,$temp_arr_keys);
                        unset($temp_arr_keys["$key"]);
                        $this->load->Model('Color_model');
                        $title=$this->Color_model->get_title($arr_query["$arr_query_key_item"])->title;
                        $temp_arr_str=site_url('search/index');
                        $index=0;
                        foreach($temp_arr_keys as $temp_arr_keys_item)
                        {
                            if($index==0)
                            {
                                $temp_arr_str.="?$temp_arr_keys_item=".$arr_query["$temp_arr_keys_item"];
                            }
                            else
                            {
                                $temp_arr_str.="&$temp_arr_keys_item=".$arr_query["$temp_arr_keys_item"];
                            }
                            $index++;
                        }
                        $condition_key_val[]=array('title'=>$title,'url'=>$temp_arr_str);
                        break;
                    default:break;
                }
            }
        }
        else
        {
            $mark=0;
        }
        return $condition_key_val;
    }

}

class Back_Controller extends MY_Controller
{
    public $first_top_level_menu;
    public function __construct()
    {
        parent::__construct();
       // $this->Check();
        $data['get_current_user_name']=$this->get_current_user_name();
        $data['1']=0;
        $data['user_authority']=$this->get_user_authority();
        $data['top_level_menu']=$this->get_top_level_menu();
        if(!empty($data['top_level_menu']))
        {
            $this->first_top_level_menu=reset($data['top_level_menu']);
        }
        $this->load->vars($data);
    }
    public function Check()
    {
        $CI =& get_instance();
        $CI->load->library('session');
        if(!$CI->session->has_userdata('user_account'))
        {
            Page_Redirect('admin/login');
        }
    }

    //获取顶级栏目
    public function get_top_level_menu()
    {
        $this->load->Model('Menu_model');
        return $this->Menu_model->get_top_level_menu();
    }
    //获取用户级别
    public function get_user_authority()
    {
        $CI =& get_instance();
        $CI->load->library('session');
        return $this->session->userdata('user_admin');
    }

    //获取当前登录用户账号名称
    public function get_current_user_name()
    {
        $CI =& get_instance();
        $CI->load->library('session');
        return $this->session->user_account;
    }
    public function Ajax_Check($ajax_model)
    {
        $CI =& get_instance();
        $CI->load->library('session');
        if(!$CI->session->has_userdata('user_account'))
        {
            switch($ajax_model->Type)
            {
                //普通跳转登陆页
                case '1':
                    Page_Redirect('admin/login');
                    break;
                //iframea jax请求
                case '2'://单层iframe框架跳转登陆页
                    Frame_Page_Redirect('admin/login');
                    break;
                case '3'://双层iframe框架跳转登陆页
                    Frame_double_Page_Redirect('admin/login');break;
                case '4'://ajax跳转登陆页
                    $ajax_model->Msg='未登录';
                    $ajax_model->Statu='f_okto';
                    $ajax_model->BackUrl=site_url('admin/login');
                    echo(json_encode($ajax_model));
                    break;
                default:break;
            }
        }
    }

    public function get_crumbs_menu($menuid)
    {
        $crumbs_url=array();
        $crumbs_url[]=array(site_url('admin/menu/index'),'栏目管理');
        if(!empty($menuid) && $menuid>0)
        {
            $this->load->Model('Menu_model');
            $menu_model= $this->Menu_model->get_menu_by_id($menuid);
            $arr_menuids= explode(',',trim($menu_model->path,','));

            foreach($arr_menuids as $menuid_item)
            {
                $crumbs_menu_model=$this->Menu_model->get_menu_by_id($menuid_item);
                $crumbs_url[]=array(site_url('admin/menu/child_menu/'.$menuid_item),$crumbs_menu_model->title);
            }
        }
        return $crumbs_url;
    }

    public function get_crumbs_content_menu($menuid)
    {
        $crumbs_url=array();
        $fisrt_top_menu=$this->first_top_level_menu;
        $contents_url=$this->get_user_authority()==1?"javascript:manage_menu()":"javascript:alert_msg()";
        if(!empty($fisrt_top_menu))
        {
            if($fisrt_top_menu->child>0)
            {
                $contents_url=site_url("admin/menu/menulist/".$fisrt_top_menu->menuid);
            }
            else
            {
                $contents_url=site_url("admin/".$fisrt_top_menu->menu_control."/".$fisrt_top_menu->menu_action."/".$fisrt_top_menu->menuid);
            }
        }
        $crumbs_url[]=array($contents_url,'内容管理');

        $menuModel=$this->Menu_model->get_menu_by_id($menuid);
        $arr_menuids= explode(',',trim($menuModel->path,','));
        foreach($arr_menuids as $menuid_item)
        {
            $crumbs_menu_model=$this->Menu_model->get_menu_by_id($menuid_item);
            $_crumbs_url=$crumbs_menu_model->child==1?site_url('admin/menu/menulist/'.$crumbs_menu_model->menuid):site_url('admin/'.$crumbs_menu_model->menu_control.'/'.$crumbs_menu_model->menu_action.($crumbs_menu_model->page_type==1?'list':'info').'/'.$crumbs_menu_model->menuid);
            $crumbs_url[]=array($_crumbs_url,$crumbs_menu_model->title);
        }
        return $crumbs_url;
    }

    public function get_max_dept_first_menu($menu_model,&$one)
    {
        if($menu_model->child==1)
        {
            $child_arr_menuids= explode(',',trim($menu_model->arrchild,','));
            $child_menu=$this->Menu_model->get_menu_by_id($child_arr_menuids[1]);
            $this->get_max_dept_first_menu($child_menu,$one);
        }else{
            $one=$menu_model;
        }
    }


}

//系统设置控制器类
class Sys_Controller extends Back_Controller
{
    public function __construct()
    {
        parent::__construct();
        $data['left']=1;
        $this->load->vars($data);
    }
}
//管理员控制器类
class User_Controller extends Back_Controller
{
    public function __construct()
    {
        parent::__construct();
        $data['left']=3;
        $this->load->vars($data);
    }
}

//产品控制器类
class Goods_Controller extends Back_Controller
{
    public function __construct()
    {
        parent::__construct();
        $data['left']=2;
        $this->load->vars($data);
    }
}

class Menu_Controller extends Back_Controller
{
    //定义顶级栏目第一个栏目
    public function __construct()
    {
        parent::__construct();
        $data['left']=4;


        $this->load->vars($data);
    }





}


?>